from web_scraper import get_html, parse_html
from data_processing import clean_data, generate_excel, generate_wordcloud, filter_by_keyword, search_by_rank
import logging


def main():
    base_url = 'https://www.shanghairanking.cn/rankings/bcur/2021'
    all_data = []
    for page in range(1, 11):  # 爬取10页的数据
        logging.info(f"正在爬取第 {page} 页...")
        url = f"{base_url}?page={page}"
        html_content = get_html(url)
        if html_content:
            parsed_data = parse_html(html_content)
            cleaned_data = clean_data(parsed_data)
            all_data.extend(cleaned_data)
        else:
            logging.warning(f"无法获取第 {page} 页的内容，跳过...")
    if not all_data:
        logging.error("没有数据可用于生成 Excel 文件和词云。")
        return

    while True:
        print("请选择要执行的操作:")
        print("1. 关键词过滤")
        print("2. 排名搜索")
        print("3. 生成词云和 Excel 文件")
        print("4. 退出程序")
        choice = input("输入操作对应的数字: ")

        if choice == '1':
            keyword = input("请输入要过滤的关键词: ")
            filtered_data = filter_by_keyword(all_data, keyword)
            logging.info(f"根据关键词'{keyword}'过滤后的数据：")
            for row in filtered_data:
                logging.info(row)
        elif choice == '2':
            rank = int(input("请输入要搜索的排名: "))
            result = search_by_rank(all_data, rank)
            if result:
                logging.info(f"排名为 {rank} 的学校信息：")
                logging.info(result[0])
            else:
                logging.info(f"未找到排名为 {rank} 的学校。")
        elif choice == '3':
            generate_wordcloud(all_data)
            generate_excel(all_data, 'university_rankings.xlsx')
        elif choice == '4':
            logging.info("程序已退出。")
            break
        else:
            logging.warning("无效的选择，请重新输入。")

if __name__ == "__main__":
    main()
